package day_6_30;

public class Main1 {
    /**
     * 86. 分隔链表
     * https://leetcode.cn/problems/partition-list/description/
     */
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode partition(ListNode head, int x) {
        // 不是在原head中进行操作，那么创建两个链表进行操作
        ListNode min = new ListNode(0);
        ListNode cur1 = min;
        ListNode max = new ListNode(0);
        ListNode cur2 = max;
        while(head != null) {
            if(head.val < x) {
                cur1.next = head;
                cur1 = cur1.next;
            }else {
                cur2.next = head;
                cur2 = cur2.next;
            }
            head = head.next;
        }
        cur1.next = max.next;
        cur2.next = null;
        return min.next;
    }
}
